package com.chunxuan.langquan.ui.fragment;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chunxuan.langquan.R;
import com.chunxuan.langquan.base.BaseResult2;
import com.chunxuan.langquan.base.Global;
import com.chunxuan.langquan.dao.bean.CourseDetail;
import com.chunxuan.langquan.dao.bean.Subject;
import com.chunxuan.langquan.dao.bean.SubjectBean2;
import com.chunxuan.langquan.dao.bean.UserInfo;
import com.chunxuan.langquan.dao.http.APIRetrofit;
import com.chunxuan.langquan.support.util.BitmapUtil;
import com.chunxuan.langquan.support.util.GlideUtil;
import com.chunxuan.langquan.support.util.Logg;
import com.chunxuan.langquan.ui.activity.AboutUsActivity;
import com.chunxuan.langquan.ui.activity.AddressManageActivity;
import com.chunxuan.langquan.ui.activity.AllRecordActivity;
import com.chunxuan.langquan.ui.activity.CouponActivity;
import com.chunxuan.langquan.ui.activity.OrderListActivity;
import com.chunxuan.langquan.ui.activity.PersonalInfoActivity;
import com.chunxuan.langquan.ui.activity.SubjectActivity;
import com.chunxuan.langquan.ui.activity.store.ShoppingCarActivity;
import com.chunxuan.langquan.ui.adapter.CourseRecordAdapter;
import com.chunxuan.langquan.ui.adapter.SubjectAdapter;
import com.makeramen.roundedimageview.RoundedImageView;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.util.ToastUtils;

import java.util.List;
import java.util.Locale;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 首页
public class Home4Fragment extends BaseFragment {
    private static Home4Fragment fragment;
    private RoundedImageView ivMyHeader;
    private TextView tvNickName,tvSchoolName,tvGrade,tvPersonalInfo,tvAllRecord;
    private RelativeLayout rlShoppingCar,rlOrderManage,rlAddressManage,rlCoupon,rlAboutUs;
    private UserInfo userInfo;
    private RecyclerView rlvCourseRecord;
    private LinearLayout llMall;
    public Home4Fragment() {

    }

    public static Home4Fragment newInstance() {
        if (fragment == null) {
            fragment = new Home4Fragment();
            Bundle args = new Bundle();
            fragment.setArguments(args);
        }
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_my, container, false);
//        banner = v.findViewById(R.id.banner);// Banner布局
        ivMyHeader=v.findViewById(R.id.my_avatar);
        tvNickName=v.findViewById(R.id.my_name);
        tvSchoolName=v.findViewById(R.id.my_school);
        tvGrade=v.findViewById(R.id.tv_my_grade);
        tvPersonalInfo=v.findViewById(R.id.tv_personal_info);
        tvAllRecord=v.findViewById(R.id.tv_all_course_record);
        rlShoppingCar=v.findViewById(R.id.rl_shopping_car);
        rlOrderManage=v.findViewById(R.id.rl_order_manage);
        rlAddressManage=v.findViewById(R.id.rl_address_manage);
        rlCoupon=v.findViewById(R.id.rl_coupon);
        rlAboutUs=v.findViewById(R.id.rl_about_us);
        rlvCourseRecord=v.findViewById(R.id.rlv_course_record);
        llMall=v.findViewById(R.id.ll_mall);
        if(Global.IS_SHOW==0){
            llMall.setVisibility(View.GONE);
        }else if(Global.IS_SHOW==1){
            llMall.setVisibility(View.VISIBLE);
        }
        rlvCourseRecord.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));

        setListeners();
        return v;
    }
    private Disposable getCourseRecord() {
        return APIRetrofit.getDefault().getCourseRecord(Global.HEADER,"")
                .compose(RxUtil.<BaseResult2<List<CourseDetail.CourseInfo>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<CourseDetail.CourseInfo>>>() {
                    @Override
                    public void accept(BaseResult2<List<CourseDetail.CourseInfo>> result2) throws Exception {
                        hideProgress();
                        if (result2.isSuccess()) {
                            if(result2.getData().size()==0){
                                if(null!=rlvCourseRecord&&null!=tvAllRecord){
                                    rlvCourseRecord.setVisibility(View.GONE);
                                    tvAllRecord.setVisibility(View.INVISIBLE);
                                    return;
                                }
                            }else{
                                Logg.e("==课程记录=="+result2.getData().get(0).getKecheng_image());
                                CourseRecordAdapter courseRecordAdapter=new CourseRecordAdapter(getContext(),result2.getData());
//                                if(null!=rlvCourseRecord){
                                    rlvCourseRecord.setAdapter(courseRecordAdapter);
                                courseRecordAdapter.notifyDataSetChanged();
//                                }
                            }

                        } else {
                            ToastUtils.showShort(result2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        if(null!=rlvCourseRecord&&null!=tvAllRecord){
                            rlvCourseRecord.setVisibility(View.GONE);
                            tvAllRecord.setVisibility(View.INVISIBLE);
                            Logg.e("==课程记录==失败==");
                        }
//                        ToastUtils.showShort("获取课程记录失败");
                    }
                });
    }
    @Override
    public void onResume() {
        super.onResume();
        getUserInfo();
        getCourseRecord();
    }

    private void setListeners() {
        setOnClickListener(tvPersonalInfo,tvAllRecord,rlShoppingCar,rlOrderManage,rlAddressManage,rlCoupon,rlAboutUs);
    }

    @Override
    public void onClick(View v) {
        if(v==tvPersonalInfo){
            if(null!=userInfo){
                Intent intent=new Intent(getActivity(),PersonalInfoActivity.class);
                intent.putExtra("avatar",userInfo.getAvatar());
                intent.putExtra("nickname",userInfo.getNickname());
                intent.putExtra("gender",userInfo.getGender());
                intent.putExtra("area",userInfo.getProv()+userInfo.getCity()+userInfo.getArea());
                intent.putExtra("grade_category_name",userInfo.getGrade_category_name());
                startActivity(intent);
            }
//            startActivity(PersonalInfoActivity.class);
        }else if(v==tvAllRecord){
            startActivity(AllRecordActivity.class);
        }else if(v==rlShoppingCar){
            startActivity(ShoppingCarActivity.class);
        }else if(v==rlOrderManage){
            startActivity(OrderListActivity.class);
        }else if(v==rlAddressManage){
            startActivity(AddressManageActivity.class);
        }else if(v==rlCoupon){
            startActivity(CouponActivity.class);
        }else if(v==rlAboutUs){
            startActivity(AboutUsActivity.class);
        }
//        if (v == rl_search) {// 搜索
//            startActivity(SearchActivity.class);
//        }
//        if (v == iv_notice) {// 消息
////            startActivityForResult(new Intent(getActivity(), MsgListActivity.class), 1001);
//        }
    }
    private Disposable getUserInfo() {
        return APIRetrofit.getDefault().userInfo(Global.HEADER,"")
                .compose(RxUtil.<BaseResult2<UserInfo>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<UserInfo>>() {
                    @Override
                    public void accept(BaseResult2<UserInfo> result2) throws Exception {
                        hideProgress();
                        if (result2.isSuccess()) {
                            userInfo=result2.getData();
                            Logg.e("==用户信息-昵称=="+result2.getData().getNickname());
                            if(!TextUtils.isEmpty(result2.getData().getNickname())){
                                tvNickName.setText(result2.getData().getNickname());
                            }else {
                                tvNickName.setText("");
                            }
                            if(!TextUtils.isEmpty(result2.getData().getSchool_name())){
                                tvSchoolName.setText(result2.getData().getSchool_name());
                            }else {
                                tvSchoolName.setText("");
                            }
                            if(!TextUtils.isEmpty(result2.getData().getGrade_category_name())){
                                tvGrade.setText(result2.getData().getGrade_category_name());
                            }else {
                                tvGrade.setText("");
                            }
                            if(!TextUtils.isEmpty(result2.getData().getAvatar())){
                                GlideUtil.loadImg(result2.getData().getAvatar(),ivMyHeader);
                            }
                        } else {
                            ToastUtils.showShort(result2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("获取用户信息失败");
                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}